import sys, os
import pickle
import numpy as np
from dataset import mnist


def sigmoid(x):
    return 1 / (1 + np.exp(-x))


def softmax(a):
    c = np.max(a)
    #  通过减去输入信号中的最大值，防止指数函数运算溢出
    exp_a = np.exp(a - c)
    sum_exp_a = np.sum(exp_a)
    y = exp_a / sum_exp_a
    return y


def get_data():
    (x_train, t_train), (x_test, t_test) = mnist.load_mnist(normalize=True,
                                                            one_hot_label=True)
    return x_test, t_train


# init_network()会读入保存在pickle文件
# sample_weight.pkl中的学习到的权重参数
def init_network():
    with open("sample_weight.pkl", 'rb') as f:
        network = pickle.load(f)
    return network


# 三层神经网络的输出
def predict(network, x):
    W1, W2, W3 = network['W1'], network['W2'], network['W3']
    b1, b2, b3 = network['b1'], network['b2'], network['b3']
    a1 = np.dot(x, W1) + b1
    z1 = sigmoid(a1)
    a2 = np.dot(z1, W2) + b2
    z2 = sigmoid(a2)
    a3 = np.dot(z2, W3) + b3
    y = softmax(a3)
    return y


#  mini-batch版交叉熵误差
def cross_entropy_error(y, t):
    if y.ndim == 1:
        # NumPy 你想要一个形状为 (1, N) 的新数组，其中 N 是原数组中的元素总数。
        # 这里的 1 表示你想要的新数组的第一维度（行数）为1，
        # 而 t.size 则表示第二维度（列数）应包含原数组的所有元素。
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
    batch_size = y.shape[0]
    delta = 1e-7
    return -np.sum(t * np.log(y + delta)) / batch_size


if __name__ == '__main__':
    x_train, t_train= get_data()
    network = init_network()
    train_size = x_train.shape[0]
    batch_size = 10
    # 随机选取10个训练样本
    batch_mask = np.random.choice(train_size, batch_size)
    x_batch = x_train[batch_mask] # (10,784)
    t_batch = t_train[batch_mask] # (10,10)
    y = predict(network, x_batch)
    print(f"交叉熵误差：{cross_entropy_error(y, t_batch)}")





